//
//  CBUserConfig.m
//  CoachsBook
//
//  Created by huiyong li on 2022/9/2.
//

#import "CBUserConfig.h"
#import "LoginUserModel.h"
#import "SWTModel.h"

@implementation CBUserConfig

static CBUserConfig *instance = nil;
+ (CBUserConfig *)shareInstance{
    @synchronized(self) {
        if (nil == instance) {
            [self new];
        }
    }
    return instance;
}
+ (id)allocWithZone:(NSZone *)zone{
    @synchronized(self) {
        if (instance == nil) {
            instance = [super allocWithZone:zone];
            return instance;
        }
    }
    return nil;
}

/*********************************************************************/
//是否直接登陆 0退出登录 1登录
- (void)saveAutoLogin:(int)index{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:AutoLogin_KEY];
    [settings setObject:[NSNumber numberWithInt:index] forKey:AutoLogin_KEY];
    [settings synchronize];
}
- (int)getAutoLogin{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:AutoLogin_KEY] intValue];
}

/*********************************************************************/
//保存登陆信息数组
- (void)saveLoginInfoArr:(NSMutableArray *)loginInfoArr{
    NSMutableArray *mArr = [NSMutableArray array];
    for ( LoginUserModel *loginInfo in loginInfoArr) {
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:loginInfo];
        [mArr addObject:data];
    }
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings setObject:mArr forKey:USERINFO];
    [settings synchronize];
}
- (NSMutableArray *)getLoginInfoArr{
    NSMutableArray *mArr = [NSMutableArray array];
    NSArray *loginArr = [[NSUserDefaults standardUserDefaults] objectForKey:USERINFO];
    for (int i = 0; i < loginArr.count; i++) {
        LoginUserModel *loginInfo = [NSKeyedUnarchiver unarchiveObjectWithData:[loginArr objectAtIndex:i]];
        [mArr addObject:loginInfo];
    }
    return mArr;
}

//用户定位城市名字
- (void)saveUser_CityName:(NSString *)cityName
{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:UCityName];
    [settings setObject:cityName forKey:UCityName];
    [settings synchronize];
}
- (NSString *)getUser_CityName
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:UCityName];
}

//用户定位城市
- (void)saveUser_CityID:(NSString *)cityID
{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:CityID];
    [settings setObject:cityID forKey:CityID];
    [settings synchronize];
}
- (NSString *)getUser_CityID
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:CityID];
}


//用户id
- (void)saveUser_id:(NSString *)user_id{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:USERID];
    [settings setObject:user_id forKey:USERID];
    [settings synchronize];
}
- (NSString *)getUser_id{
    return [[NSUserDefaults standardUserDefaults] objectForKey:USERID];
}
//验证参数userkey
- (void)saveUkey:(NSString *)ukey{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:USERKEY];
    [settings setObject:ukey forKey:USERKEY];
    [settings synchronize];
}
- (NSString *)getUkey{
    return [[NSUserDefaults standardUserDefaults] objectForKey:USERKEY];
}
//用户头像
- (void)saveHeadimg:(NSString *)headimg{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:USERHEADIMG];
    [settings setObject:headimg forKey:USERHEADIMG];
    [settings synchronize];
}
- (NSString *)getHeadimg{
    return [[NSUserDefaults standardUserDefaults] objectForKey:USERHEADIMG];
}
//用户昵称
- (void)saveNickname:(NSString *)nickname{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:USERNICKNAME];
    [settings setObject:nickname forKey:USERNICKNAME];
    [settings synchronize];
}
- (NSString *)getnickName{
    return [[NSUserDefaults standardUserDefaults] objectForKey:USERNICKNAME];
}
//手机号
- (void)saveTel:(NSString *)tel{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:USERTEL];
    [settings setObject:tel forKey:USERTEL];
    [settings synchronize];
}
- (NSString *)getTel{
    return [[NSUserDefaults standardUserDefaults] objectForKey:USERTEL];
}
//是否绑定微信
- (void)saveWechat:(NSString *)wachat{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:USERWECHAT];
    [settings setObject:wachat forKey:USERWECHAT];
    [settings synchronize];
}
- (NSString *)getWechat{
    return [[NSUserDefaults standardUserDefaults] objectForKey:USERWECHAT];
}
//用户名
- (void)saveName:(NSString *)name{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:USERNAME];
    [settings setObject:name forKey:USERNAME];
    [settings synchronize];
    
}
- (NSString *)getName{
    return [[NSUserDefaults standardUserDefaults] objectForKey:USERNAME];
}
//是否是会员
- (void)saveIsVip:(NSString *)name{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:USERISVIPP];
    [settings setObject:name forKey:USERISVIPP];
    [settings synchronize];
    
}
- (NSString *)getIsVip{
    return [[NSUserDefaults standardUserDefaults] objectForKey:USERISVIPP];
}

- (void)saveWXcode:(NSString *)WXcode
{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:USERWXcode];
    [settings setObject:WXcode forKey:USERWXcode];
    [settings synchronize];

    
}

- (NSString *)getWXcode
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:USERWXcode];
}

//存储绑定微信信息
- (void)saveWXInfoDic:(NSDictionary *)WXInfoDic{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:BANGWXINFO];
    [settings setObject:WXInfoDic forKey:BANGWXINFO];
    [settings synchronize];
}
- (NSDictionary *)getWXInfoDic{
    return [[NSUserDefaults standardUserDefaults] objectForKey:BANGWXINFO];
}

//存储用户信息
- (void)CURRENTUSERIDd:(NSDictionary *)userinfo{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:USERINFO1];
    [settings setObject:userinfo forKey:USERINFO1];
    [settings synchronize];
}
- (NSDictionary *)getUserInFoDic{
    return [[NSUserDefaults standardUserDefaults] objectForKey:USERINFO1];
}


//退出登录
- (void)LogOut
{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:USERINFO1];
    [settings removeObjectForKey:USERID];
    [settings removeObjectForKey:USERKEY];
    [settings removeObjectForKey:USERTEL];
    [settings removeObjectForKey:USERHEADIMG];
    [settings removeObjectForKey:USERINFO];
    [settings removeObjectForKey:USERNICKNAME];
    [settings removeObjectForKey:USERWECHAT];
    [settings removeObjectForKey:USERNAME];
    [settings removeObjectForKey:BANGWXINFO];
    
    [settings synchronize];
}


//是否同意隐私政策等
- (void)saveIsAgreeXieyin:(NSString *)name{
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings removeObjectForKey:ISAGREEXIEYIN];
    [settings setObject:name forKey:ISAGREEXIEYIN];
    [settings synchronize];
}
- (NSString *)getIsAgreeXieyin{
    return [[NSUserDefaults standardUserDefaults] objectForKey:ISAGREEXIEYIN];
}

//保存开关信息数组
- (void)saveSwtInfoArr:(NSArray *)swtInfoArr{
    NSMutableArray *mArr = [NSMutableArray array];
    for (ADSModel *swtModel in swtInfoArr) {
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:swtModel];
        [mArr addObject:data];
    }
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings setObject:swtInfoArr forKey:ADSINFO];
    [settings synchronize];
}
- (NSMutableArray *)getSwtInfoArr{
    NSMutableArray *mArr = [NSMutableArray array];
    NSArray *swtArr = [[NSUserDefaults standardUserDefaults] objectForKey:ADSINFO];
    for (int i = 0; i < swtArr.count; i++) {
        ADSModel *swtModel = [NSKeyedUnarchiver unarchiveObjectWithData:[swtArr objectAtIndex:i]];
        NSLog(@"%@",swtModel.img);
        [mArr addObject:swtModel];
    }
    return mArr;
}

@end
